from com.chq.panbaidu.GetHome import *
from com.chq.panbaidu.GetDownPage import *
import re, json, time
import pymysql.cursors


# 打开并处理页面，如果分享链接需要密码，则提醒用户需输入密码，否则直接进入下载页面
def update_url_music(download_url, file_name, url):
    # 获取数据连接
    connection = pymysql.connect(host="127.0.0.1", user="root", password="root", db="common", charset="utf8mb4")
    cursor = connection.cursor()
    try:
        # 创建sql语句
        sql = "update `music_51ape` set `download_url`=%s,`file_name`=%s,`flag`=2 where `url`=%s"
        # 执行sql语句
        cursor.execute(sql, (download_url, file_name, url))
        # 提交
        connection.commit()
    except Exception as e:
        # 错误回滚
        connection.rollback()
    finally:
        connection.close()


def update_flag(flag, url):
    # 获取数据连接
    connection = pymysql.connect(host="127.0.0.1", user="root", password="root", db="common", charset="utf8mb4")
    cursor = connection.cursor()
    try:
        # 创建sql语句
        sql = "update `music_51ape` set `flag`=%s where `url`=%s"
        # 执行sql语句
        cursor.execute(sql, (flag, url))
        # 提交
        connection.commit()
    except Exception as e:
        # 错误回滚
        connection.rollback()
    finally:
        connection.close()


class GetShareLink():
    loadShareLink = GetHome()
    loadDownPage = GetDownPage()
    ErrorCode = None
    headers = {
        'Accept': 'text/html, application/xhtml+xml, image/jxr, */*',
        'Accept-Language': 'zh-Hans-CN,zh-Hans;q=0.5',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36 Edge/15.15063',
        'Accept-Encoding': 'gzip, deflate, br',
        'Host': 'pan.baidu.com',
        'Connection': 'Keep-Alive'
    }

    def getShareLink(self, url, password):
        wdata = self.loadShareLink.requesHomePage(url, self.headers, 'Get', 'utf-8')
        if ('百度网盘 请输入提取密码' in wdata.text):
            print(wdata.url)
            splitPath = wdata.url.split('=')
            surl = splitPath[1]
            uid = ""
            self.verifyCode(surl, password)
            DescData = self.getDescLink(surl)
            rePattem = r'(?<=sign":").*?(?=","public")|(?<=timestamp":).*?(?=,"timeline_status")|(?<="bdstoken":).*?(?=,"is_vip)|(?<="uk":).*?(?=,"task_key")|(?<="shareid":).*?(?=,"sign)|(?<="fs_id":).*?(?=,"app_id)'
            refind = re.findall(rePattem, DescData.text)
            self.ErrorCode = self.loadDownPage.getDownPage(DescData.url, refind)
            while True:
                if (type(self.ErrorCode) != int):
                    js = json.loads(self.ErrorCode)
                    file_url = js["list"][0]['dlink']
                    file_name = js['list'][0]['server_filename']
                    print("文件下载链接：{}".format(file_url))
                    print("文件名：{}".format(file_name))
                    update_url_music(file_url, file_name, url)
                    # print('文件已经正确解析，正在启动下载中，请耐心等候，这需要较长的时间......')
                    # filePath = input('请指定文件保存的路径：（格式如：D:\）请务必添加后面的斜杠:')
                    # self.loadDownPage.DownFile(js["list"][0]['dlink'], js['list'][0]['server_filename'], filePath)
                    break
                if (self.ErrorCode == -20):
                    print('当前请求过于频繁，需要输入验证码，服务器返回的错误代码为：{}'.format(self.ErrorCode))
                    self.loadDownPage.getVerCode(DescData.url)
                    self.ErrorCode = self.loadDownPage.getDownPage(DescData.url, refind, True)
                else:
                    print('哎呀,本次请求出现异常啦，错误码为：{},将在50秒后重新请求.'.format(self.ErrorCode))
                    time.sleep(50)
                    self.ErrorCode = self.loadDownPage.getDownPage(DescData.url, refind, True)
        else:
            rePattem = r'(?<=sign":").*?(?=","public")|(?<=timestamp":).*?(?=,"timeline_status")|(?<="bdstoken":).*?(?=,"is_vip)|(?<="uk":).*?(?=,"task_key")|(?<="shareid":).*?(?=,"sign)|(?<="fs_id":).*?(?=,"app_id)'
            refind = re.findall(rePattem, wdata.text)
            if len(refind) < 6:
                update_flag(10, url)
                return
            self.ErrorCode = self.loadDownPage.getDownPage(url, refind, True)
            while True:
                if (type(self.ErrorCode) != int):
                    js = json.loads(self.ErrorCode)
                    file_url = js["list"][0]['dlink']
                    file_name = js['list'][0]['server_filename']
                    print("文件下载链接：{}".format(file_url))
                    print("文件名：{}".format(file_name))
                    update_url_music(file_url, file_name, url)
                    # print('文件已经正确解析，正在启动下载中，请耐心等候，这需要较长的时间......')
                    # filePath = input('请指定文件保存的路径：（格式如：D:\）请务必添加后面的斜杠:')
                    # self.loadDownPage.DownFile(js["list"][0]['dlink'], js['list'][0]['server_filename'], filePath)
                    break
                if (self.ErrorCode == -20):
                    print('当前请求过于频繁，需要输入验证码，服务器返回的错误代码为：{}'.format(self.ErrorCode))
                    self.loadDownPage.getVerCode(url)
                    self.ErrorCode = self.loadDownPage.getDownPage(url, refind, True)
                else:
                    print('哎呀,本次请求出现异常啦，错误码为：{},将在50秒后重新请求.'.format(self.ErrorCode))
                    time.sleep(50)
                    self.ErrorCode = self.loadDownPage.getDownPage(url, refind, True)

    def verifyCode(self, surl, pwd):
        '''
        :param surl:
        :param pwd:
        :return:
        '''
        url = r'http://pan.baidu.com/share/verify?surl={}'.format(surl)
        Referer = 'http://pan.baidu.com/share/init?surl={}'.format(surl)
        headers = {
            'Host': 'pan.baidu.com',
            'Connection': 'keep-alive',
            'Content-Length': '',
            'Accept': '*/*',
            'Origin': 'http://pan.baidu.com',
            'X-Requested-With': 'XMLHttpRequest',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'DNT': '',
            'Referer': Referer,
            'Accept-Encoding': 'gzip, deflate',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6'
        }
        data = {'pwd': pwd}
        wdata = self.loadShareLink.requesHomePage(url, headers, 'Post', 'utf-8', data)
        js = json.loads(wdata.text)
        if (js['errno'] != 0):
            print('请求失败....错误码为：{}'.format(js['errno']))
        else:
            return True

    def getDescLink(self, surl):
        url = r'https://pan.baidu.com/s/1{}'.format(surl)
        Referer = 'http://pan.baidu.com/share/init?surl={}'.format(surl)
        headers = {
            'Host': 'pan.baidu.com',
            'Connection': 'keep-alive',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
            'DNT': '1',
            'Referer': Referer,
            'Accept-Encoding': 'gzip, deflate, sdch',
            'Accept-Language': 'zh-CN,zh;q=0.8,zh-TW;q=0.6',
        }
        wdata = self.loadShareLink.requesHomePage(url, headers, 'Get', 'utf-8')
        return wdata
